﻿<PImageCaptcha CaptchaWidth="200" Outlined Label="VerifyCode" ErrorMessage="@ErrorMessage" VerifyCode="@verifyCode" OnRefresh="RefreshCode"></PImageCaptcha>

@code {
    string randomCode = "123456789";

    string calcFunCode = "+-*";

    string verifyCode = "";

    string ErrorMessage = "Verify code error";

    Random random = new Random();

    protected override Task OnInitializedAsync()
    {
        RefreshCode();
        return base.OnInitializedAsync();
    }

    private Task<string> RefreshCode()
    {
        var firstNumber = randomCode[random.Next(0, randomCode.Length)].ToString();

        var secondNumber = randomCode[random.Next(0, randomCode.Length)].ToString();

        var calcFun = calcFunCode[random.Next(0, calcFunCode.Length)].ToString();

        switch (calcFun)
        {
            case "+":
                verifyCode = (int.Parse(firstNumber) + int.Parse(secondNumber)).ToString();
                break;
            case "-":
                verifyCode = (int.Parse(firstNumber) - int.Parse(secondNumber)).ToString();
                break;
            case "*":
                verifyCode = (int.Parse(firstNumber) * int.Parse(secondNumber)).ToString();
                break;
        }
        
        StateHasChanged();
        return Task.FromResult(firstNumber + calcFun + secondNumber);
    }
}